<?php
namespace jy_newcar;
defined('BY_JYA') or exit('error');
/*
	分佣奖励
	
*/
class commission extends \plugin{
	/*
		生成奖励
	*/
	function create($order){
		$order['goods']=pdo_getall('jy_weishop_order_goods',array('orderid'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
		$goods=$order['goods'][0];
		$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','title','price'));
		$carprice=$goodsinfo['price'];
		if(!is_array($order['api_data'])){
			$order['api_data']=json_decode($order['api_data'],true);
		}
		$buytype=$order['api_data']['buytype'];
		$area_mid=$order['api_data']['area_mid'];
		$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']),array('id','fromuser'));
		$fromuser=$member['fromuser'];
		$indata=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$order['mid'],
				'agent_id'=>$fromuser,
				'area_mid'=>$area_mid,
				'ordersn'=>$order['ordersn'],
				'price'=>$order['price'],
				'carprice'=>$carprice,
				'shouprice'=>$order['api_data']['shouprice'],
				'plugin_name'=>$order['plugin_name']?:'',
				'commission_price'=>$order['price'],
				'commission_money'=>0,
				'buytype'=>$buytype,
				'type'=>0,
				'ispay'=>1,
				'tuilogid'=>0,
				'nowqi'=>0,
				'getqikey'=>0,
				'qifee'=>0,
				'qinum'=>0,
				'status'=>1,
				'createtime'=>TIMESTAMP,
		);
		//购车合伙人
		$agent=pdo_get('jy_newcar_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fromuser));
		if($agent){
			if($agent && $agent['is_agent'] && !$agent['stop']){
				$gettuiqi=$this->model('model')->gettuiqi0($agent,$order);
				$indata['type']=$gettuiqi['type'];
				//$indata['tuilogid']=$gettuiqi['id'];
				//$indata['nowqi']=$gettuiqi['nowqi'];
				//$indata['getqikey']=$gettuiqi['getqikey'];
			}
			//提车时才计算
			//$this->create_commission($agent);
		}
		$this->insert('order',$indata);
		return true;
	}
	function tiche_commission($order=array()){
		//下单会员
		$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']),array('id','fromuser'));
		$fromuser=$member['fromuser'];
		if(!$fromuser){
			//return;
		}
		$member=pdo_get('jy_newcar_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
		if(!$member['is_agent']){
			pdo_update('jy_newcar_member',array('is_agent'=>1),array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
		}
		//购车合伙人
		$agent=pdo_get('jy_newcar_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fromuser));
		if($agent){
			if($agent && $agent['is_agent'] && !$agent['stop']){
				$gettuiqi=$this->model('model')->gettuiqi0($agent,$order);
				$gettuiqi['getqikey']=pdo_count('jy_newcar_order',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$gettuiqi['id']));
				$order['type']=$gettuiqi['type'];
				$order['tuilogid']=$gettuiqi['id'];
				$order['nowqi']=$gettuiqi['nowqi'];
				$order['getqikey']=$gettuiqi['getqikey'];
				if($gettuiqi['id']){
					pdo_update('jy_newcar_order',array('type'=>$order['type'],'tuilogid'=>$order['tuilogid'],'nowqi'=>$order['nowqi'],'getqikey'=>$order['getqikey']),array('uniacid'=>$_SESSION['uniacid'],'ordersn'=>$order['ordersn']));
				}
			}
			//提车时才计算
			//$this->create_commission($agent);
		}
		$carorder=$this->get('order',array('ordersn'=>$order['ordersn'],'uniacid'=>$_SESSION['uniacid']));
		$agent=pdo_get('jy_newcar_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$carorder['area_mid']));
		if($agent){
			$this->area_commission($agent,$order);
		}
		return;
	}
	//按揭后计算奖励
	function anji_commission($carorder=array()){
		$member=pdo_get('jy_newcar_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$carorder['agent_id']));
		if($member && $member['is_agent'] && !$member['stop']){
			$gettuiqi=$this->model('model')->getistuiqi($member,$carorder);
			if(!$gettuiqi['nowqi']){
				return 0;
			}
			$carorder['type']=$gettuiqi['type'];
			$carorder['tuilogid']=$gettuiqi['id'];
			$gettuiqi['num']=pdo_count('jy_newcar_order',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$gettuiqi['id']));
			$carorder['getqikey']=$gettuiqi['num'];
			$k=$carorder['getqikey'];
			$toqinum=$this->setting['g_fee'.$k];
			//计算空挡期
			if($k==1){
				if($gettuiqi['nowqi']<=$toqinum){
					$toqinum=$toqinum-$gettuiqi['nowqi']+1;
				}elseif($gettuiqi['nowqi']<=$this->setting['g_fee2']+$this->setting['g_fee1']){
					$qi2num=$this->setting['g_fee2']+$this->setting['g_fee1']-$gettuiqi['nowqi']+1;
					$toqinum=$qi2num>$toqinum?$toqinum:$qi2num;
				}elseif($gettuiqi['nowqi']<=$this->setting['g_fee3']+$this->setting['g_fee2']+$this->setting['g_fee1']){
					$qi3num=$this->setting['g_fee3']+$this->setting['g_fee2']+$this->setting['g_fee1']-$gettuiqi['nowqi']+1;
					$toqinum=$qi3num>$toqinum?$toqinum:$qi3num;
				}else{
					$toqinum=0;
				}
			}elseif($k==2){
				if($gettuiqi['nowqi']<=$toqinum){
					$gettuiqi['nowqi']=$this->setting['g_fee1']+1;
					$toqinum=$this->setting['g_fee2'];
				}elseif($gettuiqi['nowqi']<=$this->setting['g_fee2']+$this->setting['g_fee1']){
					$qi2num=$this->setting['g_fee2']+$this->setting['g_fee1']-$gettuiqi['nowqi']+1;
					$toqinum=$qi2num>$toqinum?$toqinum:$qi2num;
				}elseif($gettuiqi['nowqi']<=$this->setting['g_fee3']+$this->setting['g_fee2']+$this->setting['g_fee1']){
					$qi3num=$this->setting['g_fee3']+$this->setting['g_fee2']+$this->setting['g_fee1']-$gettuiqi['nowqi']+1;
					$toqinum=$qi3num>$toqinum?$toqinum:$qi3num;
				}else{
					$toqinum=0;
				}
			}elseif($k==3){
				if($gettuiqi['nowqi']<=$toqinum){
					$gettuiqi['nowqi']=$this->setting['g_fee1']+$this->setting['g_fee2']+1;
					$toqinum=$this->setting['g_fee3'];
				}elseif($gettuiqi['nowqi']<=$this->setting['g_fee2']+$this->setting['g_fee1']){
					$gettuiqi['nowqi']=$this->setting['g_fee1']+$this->setting['g_fee2']+1;
					$toqinum=$this->setting['g_fee3'];
				}elseif($gettuiqi['nowqi']<=$this->setting['g_fee3']+$this->setting['g_fee2']+$this->setting['g_fee1']){
					$qi3num=$this->setting['g_fee3']+$this->setting['g_fee2']+$this->setting['g_fee1']-$gettuiqi['nowqi']+1;
					$toqinum=$qi3num>$toqinum?$toqinum:$qi3num;
				}else{
					$toqinum=0;
				}
			}
			$carorder['nowqi']=$gettuiqi['nowqi'];
			$qinum=$this->setting['qinum']?:36;
			if($k==3){
				$carprice=pdo_getsum('jy_newcar_order',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$gettuiqi['id']),'carprice');
				$qifee=pdo_getsum('jy_newcar_order',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$gettuiqi['id']),'qifee');
				$lastfee=$carprice*0.8/3-$qifee;
				$money=$lastfee/$toqinum;
			}else{
				$lastfee=$carorder['carprice']*0.8;
				$money=$lastfee/$qinum;
			}
			$red_money=$toqinum*$money;
			$carorder['qinum']=$toqinum;
			$carorder['qifee']=$red_money;
			if($red_money>0 && $gettuiqi['id']){
				//购车合伙人才产生分期；
				if(($this->setting['isfee2'] && $gettuiqi['type']) || !$gettuiqi['type']){
					$carorder['commission_money']+=$red_money;
					$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$member['mid'],
							'ordersn'=>$carorder['ordersn'],
							'tuilogid'=>$carorder['tuilogid'],
							'money'=>$red_money,
							'qinum'=>$toqinum,
							'istui'=>$k,
							'type'=>$gettuiqi['type']?2:0,
							'createtime'=>TIMESTAMP,
							'status'=>0,
					);
					$this->insert('commission',$new_commission);
					$id=pdo_insertid();
					//分期状态：
					$yufee=$money;
					$this->model('yufee')->yufeepay($carorder,$gettuiqi['id'],$yufee);
					$this->update('commission',array('status'=>2,'complete_time'=>TIMESTAMP),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
				}
			}
			if(!$gettuiqi['type']){
				$this->update('order',array('qinum'=>$carorder['qinum'],'qifee'=>$carorder['qifee'],'nowqi'=>$gettuiqi['nowqi']),array('id'=>$carorder['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	//卖车HHR推广奖励
	function type1_commission($carorder=array()){
		$member=pdo_get('jy_newcar_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$carorder['agent_id']));
		if($member && $member['is_agent'] && !$member['stop']){
			$gettuiqi=$this->model('model')->gettuiqi1($member,$carorder);
			if(!$gettuiqi['id']){
				return '';
			}
			$gettuiqi['getqikey']=pdo_count('jy_newcar_order',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$gettuiqi['id']));
			pdo_update('jy_newcar_order',array('type'=>$gettuiqi['type'],'tuilogid'=>$gettuiqi['id'],'nowqi'=>$gettuiqi['nowqi'],'getqikey'=>$gettuiqi['getqikey']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$carorder['id']));
			$gettuiqi['num']=pdo_count('jy_newcar_order',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$gettuiqi['id']));
			$k=$gettuiqi['num']-1;
			$fee_set1=$this->setting['fee_set1'];
			//卖车HHR直推循环奖
			$red_money=$fee_set1[$k]['fee'];
			if($red_money>0 && $member['type']){
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'ordersn'=>$carorder['ordersn'],
						'tuilogid'=>$gettuiqi['id'],
						'money'=>$red_money,
						'istui'=>$k,
						'type'=>1,
						'createtime'=>TIMESTAMP,
						'status'=>1,
				);
				$this->insert('commission',$new_commission);
				$id=pdo_insertid();
				$this->model('yufee')->up_tuifeetixian($id);
			}
			if($gettuiqi['num']>=3){
				$this->anji_commission2($carorder,1,$gettuiqi);
			}
		}
		return 0;
	}
	//卖车合伙人分期
	function anji_commission2($carorder=array(),$type=0,$gettuiqi=array()){
		$member=pdo_get('jy_newcar_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$carorder['agent_id']));
		if($member && $member['is_agent'] && !$member['stop']){
			$gettuiqi=$gettuiqi?:pdo_get('jy_newcar_tuilog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$carorder['tuilogid']));
			$nowmonth=date('m',$gettuiqi['starttime']);
			$nowyear=date('Y',$gettuiqi['starttime']);
			for($i=0;$i<36;$i++){
					$yuestart = mktime(0,0,0,$nowmonth+$i,1,date('Y'));
					$yueend = mktime(0,0,0,$nowmonth+$i+1,1,date('Y'));
					if($yueend>TIMESTAMP){
						$nowqi=$i+1;
						break;
					}
			}
			$qinum=36-$nowqi;
			$qinum=$nowqi==36?1:$qinum;
			$alllist=pdo_getall('jy_newcar_order',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$gettuiqi['id']));
			$carprice=0;
			$allfee2=0;
			$isqinum=0;
			foreach($alllist as $av){
				$carprice +=$av['carprice'];
				$fee2=pdo_getsum('jy_newcar_commission',array('type'=>1,'uniacid'=>$_SESSION['uniacid'],'ordersn'=>$av['ordersn']),'money');
				$allfee2 +=$fee2;
			}
			$carfee=$carprice*0.8/3;
			$lastfee=$carfee-$allfee2;
			$money=$lastfee/$qinum;
			foreach($alllist as $k=>$av){
				$i=$k+1;
				$toqinum=$this->setting['g_fee'.$i];
				$isqinum+=$toqinum;
				$nowqinum=$qinum>$isqinum?$toqinum:$qinum-$isqinum+$toqinum;
				if($nowqinum>0){
					pdo_update('jy_newcar_order',array('mqinum'=>$nowqinum,'mqifee'=>$nowqinum*$money),array('uniacid'=>$_SESSION['uniacid'],'id'=>$av['id']));
				}else{
					pdo_update('jy_newcar_order',array('mqinum'=>0,'mqifee'=>0),array('uniacid'=>$_SESSION['uniacid'],'id'=>$av['id']));
				}
			}
			$carorder['qinum']=$qinum;
			$carorder['qifee']=$lastfee;
			$carorder['nowqi']=$nowqi;
			//print_r($carorder);exit;
			if($lastfee>0 && $gettuiqi['id']){
				//购车合伙人才产生分期；
				if($this->setting['isfee2'] && $gettuiqi['type']){
					$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$member['mid'],
							'ordersn'=>$carorder['ordersn'],
							'tuilogid'=>$carorder['tuilogid'],
							'money'=>$lastfee,
							'qinum'=>$qinum,
							'type'=>2,
							'createtime'=>TIMESTAMP,
							'complete_time'=>TIMESTAMP,
							'status'=>2,
					);
					$this->insert('commission',$new_commission);
					//分期状态：
					$this->model('yufee')->maiyufee($carorder,$gettuiqi['id']);
				}
			}
		}
	}
	//经销商业绩
	function area_commission($member,$order=array()){
		if($member && $member['is_area'] && !$member['stop']){
			$area_fee=$this->setting['area_fee'];
			//经销商代理收益
			$this->commission_order['commission_money']+=$area_fee;
			$new_commission=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$member['mid'],
				'ordersn'=>$order['ordersn'],
				'money'=>$area_fee,
				'type'=>4,
				'createtime'=>TIMESTAMP,
				'status'=>1,
			);
			$this->insert('commission',$new_commission);
		}
		return 0;
	}
	public function order_complete($order){
		$this->update('commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('type >'=>0,'ordersn'=>$order['ordersn'],'status'=>0,'uniacid'=>$_SESSION['uniacid']));
		$carorder=$this->get('order',array('ordersn'=>$order['ordersn'],'uniacid'=>$_SESSION['uniacid']));
		$this->type1_commission($carorder);
		$this->model('model')->uptuiqi($carorder['tuilogid']);
		return 0;
	}
	/*
		创建结算单
	*/
	public function create_balance($mid,$post=array(),$type=0){
		if(!$mid){
			exi('请先选择要结算的会员！','error');
		}
		if($post['pay_type']=='downline'){
			if(!$post['wechat']){
				exi('请先填写微信号！','error');
			}
			if(!$post['mobile']){
				exi('请先填写联系电话！','error');
			}
		}
		if($post['pay_type']=='alipay'){
			if(!$post['realname']){
				exi('请先填写真实姓名！','error');
			}
			if(!$post['alipay']){
				exi('请先填写支付宝帐号！','error');
			}
			if(!$post['alipay1']){
				exi('请确认帐号','error');
			}
			if($post['alipay1']!=$post['alipay']){
				exi('确认帐号与支付宝帐号不匹配！','error');
			}
			$typetitle='支付宝';
		}
		if($post['pay_type']=='union'){
			if(!$post['realname']){
				exi('请输入姓名','error');
			}
			if(!$post['bankname']){
				exi('请输入所属银行','error');
			}
			if(!$post['bankcard']){
				exi('请银行卡号','error');
			}
			if(!$post['bankcard1']){
				exi('请确认卡号','error');
			}
			if($post['bankcard']!=$post['bankcard1']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
			$typetitle='银行卡';
		}
		//获取可结算奖励
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'type'=>$type==13?array(1,3):$type,'status'=>1);
		//print_r($params);exit;
		$money=pdo_getsum('jy_newcar_commission',$params,'money');
		if($money<=0){
			exi('没有可结算的奖励！','error');
		}
		$commission_num=$this->count('commission',$params,'id');
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'money'=>$money,
			'pay_type'=>$post['pay_type'],
			'realname'=>$post['realname'],
			'alipay'=>$post['alipay'],
			'bankname'=>$post['bankname'],
			'bankcard'=>$post['bankcard'],
			'wechat'=>$post['wechat'],
			'mobile'=>$post['mobile'],
			'type'=>$type,
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		//结算手续费
		if($this->setting['balance_per'] && ($post['pay_type']!='credit' || !$this->setting['balance_credit'])){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		$update=array(
			'status'=>2,
			'balancesn'=>$new_balance['balancesn'],
		);
		$this->update('commission',$update,$params);
		return $balance_id;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		$this->update('balance',$update,array('id'=>$balance_id));
		return true;
		/*$update=array(
			'status'=>1,
			'balancesn'=>'',
		);
		$balance=$this->get('balance',array('id'=>$balance_id),array('mid','balancesn'));
		if($balance['balancesn']){
			$this->update('commission',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$balance['mid'],'balancesn'=>$balance['balancesn']));
		}*/
		return true;
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type='',$wxgid='',$pay_trade_no=''){
		$balance=$this->get('balance',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			//exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['final_money'],$balance['balancesn'],$wxgid);
		$update=array(
			'status'=>3,
			'pay_trade_no'=>$trade_no=='pay_trade_no'?$pay_trade_no:$trade_no,
			'pay_time'=>TIMESTAMP,
		);
		if($pay_type){
			$update['pay_type']=$pay_type;
		}
		$this->update('balance',$update,array('id'=>$balance['id']));
		$this->model('notice')->notice_fenxiao_dakuan($balance['mid'],$balance['final_money']);
		return true;
	}
	/*
		线下打款
	*/
	protected function pay_balance_by_downline(){
		return 'pay_trade_no';
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,'jy_newcar','分销奖励打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn,$wxgid=''){
		if($wxgid){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$wxgid,'uniacid'=>$_SESSION['uniacid']),array('appid','mchid','paykey'));
		}
		$params=array(
			'appid'=>$wxapp['appid']?:'',
			'mch_id'=>$wxapp['mchid']?:'',
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'wxgid'=>$wxgid,
			'remark'=>'代理商奖励发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		if(!$res['payment_no'] && !$res['batch_id']){
			exi('打款到微信零钱失败！','error');
		}
		return $res['payment_no']?:$res['batch_id'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到银行账户！','error');
	}
}